package com.zqh.productimgquery.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zqh.myfmmall.entity.ProductImg;
import com.zqh.myfmmall.result.Result;
import com.zqh.productimgquery.dao.ProductImgDao;
import com.zqh.productimgquery.service.ProductImgQueryService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;


/**
 * @Description :
 * @Author :kono淇杭哒、
 * @Create 2022-02-18 2:19
 */
@Service("productImgQueryService")
public class ProductImgQueryServiceImpl extends ServiceImpl<ProductImgDao, ProductImg> implements ProductImgQueryService{


    @Override
    public Result getMainImgUrlByProductId(String productId) {
        ProductImg mainImg = query().eq("product_id", productId).eq("is_main", 1).select("url").one();
        return Result.success(mainImg.getUrl());
    }

    @Override
    public Result getMainImgUrlByProductIds(List<String> productIds) {
        List<ProductImg> mainImgs = query().in("product_id", productIds).eq("is_main", 1).select("url").list();
        List<String> urls = mainImgs.stream().map(s -> s.getUrl()).collect(Collectors.toList());
        return Result.success(urls);
    }

    @Override
    public Result retrieveByProductId(String productId) {
        List<ProductImg> productImgs = query().eq("product_id", productId).orderByAsc("sort").list();
        return Result.success(productImgs);
    }
}
